import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_driver/http/api.dart';

import '../../AppConfig/Config.dart';
import '../../AppConfig/RouteAct.dart';
import 'AddbankCard.dart';

class FreightSettlement extends StatefulWidget {
  const FreightSettlement({super.key, required this.billMap});
  final Map billMap;
  @override
  State<FreightSettlement> createState() => _FreightSettlementState();
}

class _FreightSettlementState extends State<FreightSettlement> {
  int chooseIndex=-1;
  int cardIndex=0;
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        backgroundColor: Colors.white,
        centerTitle: true,
        title: Text("结算",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
      ),
      body: Column(
        children: [
          Expanded(child: ListView(
            children: [
              Container(
                margin: EdgeInsets.only(top: 8.h,left: 10.w,right: 10.w),
                padding: EdgeInsets.only(top: 15.h,bottom: 15.h),
                decoration: BoxDecoration(
                    color:Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Column(
                          children: [
                            SizedBox(height: 3.h,),
                            Text("结算金额¥",style: TextStyle(color: Colors.red,fontSize: 13.sp,fontWeight: FontWeight.w600),),
                          ],
                        ),

                        Text(widget.billMap['allMoney'],style: TextStyle(color: Colors.red,fontSize: 20.sp,fontWeight: FontWeight.w600),),
                      ],
                    ),

                    Container(
                      width: screenSize.width-20.w,
                      margin: EdgeInsets.only(top: 15.h),
                      padding: EdgeInsets.only(top: 8.h,bottom: 8.h,left: 10.w),
                      color: Color.fromRGBO(251, 236, 228, 1),
                      child: Text("结算到：",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp,fontWeight: FontWeight.w600),),
                    ),
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(top: 15.h),
                        padding: EdgeInsets.only(left: 10.w,right: 10.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset("lib/Images/OrderList/余额支付.png",width: 30.w,height: 30.h,),
                                SizedBox(width: 8.w,),
                                Text("余额",style: TextStyle(color: Colors.black,fontSize: 16.sp,),),
                              ],
                            ),
                            Icon(chooseIndex==1?Icons.check_circle:Icons.radio_button_off,color:chooseIndex==1? MyColors.themeColor:MyColors.authTipColor,size: 25,),
                          ],
                        ),
                      
                      ),
                      onTap: (){
                        setState(() {
                          chooseIndex=1;
                        });
                      },
                    ),
                    Divider(height: 10.h,color: MyColors.backgreyColor,),
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(top: 10.h),
                        padding: EdgeInsets.only(left: 10.w,right: 10.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset("lib/Images/OrderList/银行卡支付.png",width: 30.w,height: 30.h,),
                                SizedBox(width: 8.w,),
                                Text("招商银行储蓄卡(9999)",style: TextStyle(color: Colors.black,fontSize: 16.sp,),),
                              ],
                            ),
                            Icon(chooseIndex==2?Icons.check_circle:Icons.radio_button_off,color:chooseIndex==2? MyColors.themeColor:MyColors.authTipColor,size: 25,),
                          ],
                        ),

                      ),
                      onTap: (){
                        setState(() {
                          chooseIndex=2;
                        });
                      },
                    ),
                    Divider(height: 10.h,color: MyColors.backgreyColor,),
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(top: 10.h),
                        padding: EdgeInsets.only(left: 10.w,right: 10.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                          Text("更换银行卡、绑定新卡",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                          Icon(Icons.arrow_forward_ios,size: 14,color: MyColors.authTipColor,),
                        ],
                      ),
                    ),
                      onTap: (){
                        showModalBottomSheet(
                            context: context,
                            builder: (BuildContext context) {
                              return Container(
                                padding: EdgeInsets.only(top: 10.h,left: 10.w,right: 10.w),
                                height: 300.h,
                                width: screenSize.width,
                                decoration: BoxDecoration(
                                    color:Colors.white,
                                    borderRadius: BorderRadius.all(Radius.circular(10))),
                                child: Column(
                                  children: [
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        SizedBox(width: 15.w,),
                                        Text("选择银行卡",style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                                        IconButton(onPressed: (){Navigator.pop(context);}, icon: Icon(Icons.close,color: MyColors.textColor,size: 30,)),
                                      ],
                                    ),
                                    Divider(height: 16.h,color: MyColors.backgreyColor,),
                                    Container(
                                      height: 135.h,
                                      child: ListView.builder(
                                          itemCount: 3,
                                          itemBuilder: (context, index) {
                                            return GestureDetector(
                                              child:  Column(
                                                children: [
                                                  Row(
                                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                    children: [
                                                      Row(
                                                        children: [
                                                          Image.asset("lib/Images/PersonCenter/中国银行icon.png",width: 30.w,height: 30.h,),
                                                          SizedBox(width: 8.w,),
                                                          Text("中国银行储蓄卡(1234)",style: TextStyle(color: MyColors.textColor,fontSize: 16.sp),),
                                                        ],
                                                      ),
                                                      if( cardIndex==index)  Icon(Icons.check_circle,color: MyColors.themeColor,),
                                                    ],
                                                  ),
                                                  Divider(height: 16.h,color: MyColors.backgreyColor,),
                                                ],
                                              ),
                                              onTap: (){
                                                setState(() {
                                                  cardIndex=index;
                                                  Navigator.pop(context);
                                                });
                                              },
                                            );
                                          }
                                      ),
                                    ),
                                    GestureDetector(
                                      child: Container(
                                        child:  Row(
                                          children: [
                                            Icon(Icons.control_point,color: MyColors.textColor,),
                                            Text("添加银行卡",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                                          ],
                                        ),
                                      ),
                                      onTap: (){
                                        Navigator.pop(context);
                                        Navigator.push( context, FadeRoute(page:AddBankCard()));
                                      },
                                    )

                                  ],
                                ),
                              );
                            }
                        );
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
          ),
          Container(height: 85.h,
          color: Colors.white,
          padding: EdgeInsets.only(top: 10.h,left: 15.w,right: 15.w,bottom: 40.h),
            child:  BrnSmallMainButton(
              title: '提交结算',
              bgColor: MyColors.themeColor,
              textColor: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              width: screenSize.width-40.w,
              radius: 30,
              onTap: () {
                if(chooseIndex==1){
                  ApiManager.postData(
                    context,
                    'api/pay/driver/freight/clearFreight',
                    {
                      "type": 0,
                      "billIdList": widget.billMap['billIdList'],
                    },
                        (responseData) {
                      BrnToast.show(
                        "提交成功",
                        context,
                        duration: BrnDuration.short,
                      );
                      Navigator.pop(context, "success");
                    },
                  );
                }
              },
            ),

          )
        ],
      ),
    );
  }
}
